package com.ljkj.cordial.chat.dao.notify;


import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.DbConverterList;
import com.ljkj.cordial.chat.dao.GroupTableDao;
import com.ljkj.cordial.chat.util.DeleteHistoryUtil;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;

import java.util.List;

/**
 * 群信息
 */
@Entity(nameInDb = "GroupTable")
public class GroupTable {
    public final static String OWNER_TYPE_REGISTER = IMSContacts.UserType.REGISTERED_USER;
    public final static String OWNER_TYPE_VISITOR = IMSContacts.UserType.TOURISTS_USER;
    @Id(autoincrement = true)
    private Long id;

    /**
     * 群主Id
     */
    @Property(nameInDb = "GROUP_OWNER_ID")
    private String groupOwnerId;
    // 群组ID
    @Property(nameInDb = "GROUP_ID")
    private String groupId;

    // 群组名称
    @Property(nameInDb = "GROUP_NAME")
    private String groupName;

    // 群组头像
    @Property(nameInDb = "GROUP_AVATAR")
    private String groupAvatar;

    // 群人数
    @Property(nameInDb = "MEMBER_COUNT")
    private int memberCount;

    // 群类型(游客用户群，注册用户群)
    @Property(nameInDb = "GROUP_TYPE")
    private String groupType;

    // 群公告ID
    @Property(nameInDb = "ANNOUNCEMENT_ID")
    private String announcementId;

    //群公告标题
    @Property(nameInDb = "ANNOUNCEMENT_TITLE")
    private String announcementTitle;

    //群公告内容
    @Property(nameInDb = "ANNOUNCEMENT_CONTENT")
    private String announcementContent;

    // 阅后即焚
    @Property(nameInDb = "SNAP_CHAT_STATUS")
    private boolean snapchatStatus;

    // 阅后即焚时长
    @Property(nameInDb = "SNAP_CHAT_LENGTH")
    private int snapchatTimeLength;

    // 阅后即焚时间单位
    @Property(nameInDb = "SNAP_CHAT_TIME_UNIT")
    private int snapchatTimeUnit;
    //    @Property
//    @Convert(converter = GConverter.class, columnType = String.class)
//    private List<GroupMemberTable> members;
    @Property(nameInDb = "LOGIN_USER_ID")
    private String loginUserId;

    @Generated(hash = 610922928)
    public GroupTable() {
    }

    @Generated(hash = 1176982273)
    public GroupTable(Long id, String groupOwnerId, String groupId, String groupName, String groupAvatar, int memberCount,
                      String groupType, String announcementId, String announcementTitle, String announcementContent,
                      boolean snapchatStatus, int snapchatTimeLength, int snapchatTimeUnit, String loginUserId) {
        this.id = id;
        this.groupOwnerId = groupOwnerId;
        this.groupId = groupId;
        this.groupName = groupName;
        this.groupAvatar = groupAvatar;
        this.memberCount = memberCount;
        this.groupType = groupType;
        this.announcementId = announcementId;
        this.announcementTitle = announcementTitle;
        this.announcementContent = announcementContent;
        this.snapchatStatus = snapchatStatus;
        this.snapchatTimeLength = snapchatTimeLength;
        this.snapchatTimeUnit = snapchatTimeUnit;
        this.loginUserId = loginUserId;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public String getGroupId() {
        return this.groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return this.groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public int getMemberCount() {
        return this.memberCount;
    }

    public void setMemberCount(int memberCount) {
        this.memberCount = memberCount;
    }

    public String getGroupType() {
        return this.groupType;
    }

    public void setGroupType(String groupType) {
        this.groupType = groupType;
    }

    public String getAnnouncementId() {
        return this.announcementId;
    }

    public void setAnnouncementId(String announcementId) {
        this.announcementId = announcementId;
    }

    public String getAnnouncementTitle() {
        return this.announcementTitle;
    }

    public void setAnnouncementTitle(String announcementTitle) {
        this.announcementTitle = announcementTitle;
    }

    public String getAnnouncementContent() {
        return this.announcementContent;
    }

    public void setAnnouncementContent(String announcementContent) {
        this.announcementContent = announcementContent;
    }

    public boolean getSnapchatStatus() {
        return this.snapchatStatus;
    }

    public void setSnapchatStatus(boolean snapchatStatus) {
        this.snapchatStatus = snapchatStatus;
    }

    public int getSnapchatTimeLength() {
        return this.snapchatTimeLength;
    }

    public void setSnapchatTimeLength(int snapchatTimeLength) {
        this.snapchatTimeLength = snapchatTimeLength;
    }

    public int getSnapchatTimeUnit() {
        return this.snapchatTimeUnit;
    }

    public void setSnapchatTimeUnit(int snapchatTimeUnit) {
        this.snapchatTimeUnit = snapchatTimeUnit;
    }


    public static class GConverter extends DbConverterList<GroupMemberTable> {
    }

    public static long insert(GroupTable groupTable) {
        if (groupTable.getId() != null) {
            DbController.getInstance().getWrite().update(groupTable);
            return groupTable.getId().longValue();
        }
        groupTable.setLoginUserId(ChatApp.getInstance().getUserId());
        return DbController.getInstance().getWrite().insertOrReplace(groupTable);
    }

    public static long insertNoUpdate(GroupTable groupTable) {
        if (groupTable.getId() != null) {
            return groupTable.getId().longValue();
        }
        groupTable.setLoginUserId(ChatApp.getInstance().getUserId());
        return DbController.getInstance().getWrite().insert(groupTable);
    }

    public static GroupTable queryRowByGroupId(String groupId) {
        return DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.GroupId.eq(groupId)
                        , GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
    }

    public static GroupTable queryRowByGroupIdNotNull(String groupId) {
        GroupTable g = queryRowByGroupId(groupId);
        return g == null ? new GroupTable() : g;
    }

    public static void deleteByGroupId(String groupId) {
        GroupTable b = DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.GroupId.eq(groupId)
                        , GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (b != null) {
            DbController.getInstance().getWrite().getGroupTableDao().delete(b);
            DeleteHistoryUtil.getInstance().deleteNavChat(groupId, Canstants.GROUP_CHAT, false, true);
        }

    }

    public static void addMemberCount(String groupId) {
        GroupTable b = DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.GroupId.eq(groupId)
                        , GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (b != null && b.getMemberCount() > 0) {
            b.setMemberCount(b.getMemberCount() + 1);
            DbController.getInstance().getWrite().getGroupTableDao().update(b);
        }
    }

    public static void subMemberCount(String groupId) {
        GroupTable b = DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.GroupId.eq(groupId)
                        , GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (b != null && b.getMemberCount() > 0) {
            b.setMemberCount(b.getMemberCount() - 1);
            DbController.getInstance().getWrite().getGroupTableDao().update(b);
        }
    }

    public static List<GroupTable> query() {
        return DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<GroupTable> query(String like) {
        return DbController.getInstance().getRead().getGroupTableDao()
                .queryBuilder().where(GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).whereOr(
                        GroupTableDao.Properties.GroupName.like("%" + like + "%")
                        , GroupTableDao.Properties.GroupId.like("%" + like + "%")
                )
                .list();
    }

    public static List<GroupTable> search(String keyword) {
        keyword = "%" + keyword + "%";
        return DbController.getInstance().getRead()
                .getGroupTableDao().queryBuilder()
                .where(GroupTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()))
                .whereOr(GroupTableDao.Properties.GroupName.like(keyword), GroupTableDao.Properties.GroupId.like(keyword))
                .list();
    }

    public String getGroupOwnerId() {
        return this.groupOwnerId;
    }

    public void setGroupOwnerId(String groupOwnerId) {
        this.groupOwnerId = groupOwnerId;
    }

    public String getLoginUserId() {
        return this.loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    public String getGroupAvatar() {
        return this.groupAvatar;
    }

    public void setGroupAvatar(String groupAvatar) {
        this.groupAvatar = groupAvatar;
    }
}
